package org.abacus.platform.grid.impl;

import java.util.Calendar;
import java.util.Date;

import org.abacus.platform.grid.ValueMap;

public class ValueMapImpl implements ValueMap {
	/**
	 *
	 */
	private static final long serialVersionUID = -8613220706168408570L;
	private String key;
	private Object value;

	public String getKey() {
		return key;
	}

	public Object getValue() {
		return value;
	}

	public void setKey(String arg0) {
		key = arg0;
	}

	public void setValue(Object arg0) {
		value = arg0;
	}

	public String toJson(StringBuffer arg0) {
		arg0.append('"');
		arg0.append(key);
		arg0.append('"');
		arg0.append(':');
		if (null == value) {
			arg0.append("null");
			return arg0.toString();
		}
		if (value instanceof Calendar) {
			arg0.append(((Calendar) value).getTimeInMillis());
			return arg0.toString();
		}
		if (value instanceof Date) {
			arg0.append(((Date) value).getTime());
			return arg0.toString();
		}
		boolean flag = ((value instanceof Number) || (value instanceof Boolean) || value.getClass().isPrimitive()) && value.getClass() != char.class;

		if (!flag)
			arg0.append('"');
		arg0.append(value);
		if (!flag)
			arg0.append('"');

		return arg0.toString();
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		return toJson(sb);
	}
}
